import { Utils } from "../common/Utils"
import { Bonus } from "./Bonus"

/**邮件消息 */
export class Message{
    id:number
    /**内容 */
    content:string
    /**收件人 */
    dst:string
    sn:string
    /**发件人 */
    src:string
    /**状态：0 == 未读，1 == 已读 */
    state:number
    time:number
    type:number
    bonus:Bonus[] = []
    con:string

    static createByObj(obj:any):Message{
        let m = new Message()
        for(let key in obj){
            m[key] = obj[key]   
        }
        
        let json = JSON.parse(m.content)
        m.type = json.type

        if(json.info.bonus){
            for(let b of json.info.bonus){
                let bon = Bonus.createByObj(b)
                m.bonus.push(bon)
            }
        }

        if(json.info.con){
            m.con = json.info.con.replace(/<[^>]+>/g, '')
        }
        
        return m
    }

    get title():string{
        if(!this.con){
            return '通知'
        }

        return this.con.split(':')[0]
    }

    get isRead():boolean{
        return this.state == 1
    }

    get date():string{

        return Utils.formatDate(new Date(this.time * 1000) )
    }

    get desc():string{
        if(this.con){
            return this.con.split(':')[1]
        }
        let str = '[已获取] '
        if(this.bonus.length > 0){
            for(let b of this.bonus){
                str += b.name + ':' + b.num + ' '
            }
        }

        return str
    }


}